{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2009 --> done.\n",
      "2010 --> done.\n",
      "2011 --> done.\n",
      "2012 --> done.\n",
      "2013 --> done.\n",
      "2014 --> done.\n",
      "2015 --> done.\n",
      "2016 --> done.\n",
      "2017 --> done.\n",
      "2018 --> done.\n",
      "ok\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "def get_tweets(year):\n",
    "    url = ('http://www.trumptwitterarchive.com/data/realdonaldtrump/%s.json' %year)\n",
    "    r = requests.get(url)\n",
    "    print(str(year) + ' --> ' + 'done.')\n",
    "    return r.json()\n",
    "\n",
    "def save_tweets(years=range(2009,2019)):\n",
    "        \n",
    "        tweets = []\n",
    "        for y in years:\n",
    "            data = get_tweets(y)\n",
    "            dl = len(data)\n",
    "            \n",
    "            i = 0\n",
    "            while i < dl:\n",
    "                tweets.append(data[i])\n",
    "                i += 1\n",
    "                \n",
    "        return tweets\n",
    "    \n",
    "with open(\"trump_tweets_2009~2018.json\", \"w\", encoding=\"utf-8\") as outfile:\n",
    "    json.dump(save_tweets(), outfile)\n",
    "    print('ok')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
